package com.zheng.common.util;



import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


/**
 * @author lvg EMIAL：<a href="mailto:weiguo.liang@gh-ca.com">weiguo.liang@gh-ca.com</a> <br>
 * @version 1.0 <br>
 * @Company: GH-CA<br>
 * @ClassName: HttpUtil <br>
 * @Description: TODO <br>
 * @date 2016/12/12 0:22<br>
 **/
public class CsvUtils {

    /**
     * 获取总行数
     * @param file
     * @return
     */
    public static int getFileLineCount(File file) {
        int cnt = 0;
        LineNumberReader reader = null;
        try {
            reader = new LineNumberReader(new InputStreamReader(new FileInputStream(file)));
            while ((reader.readLine()) != null) {
            }
            cnt = reader.getLineNumber();
        } catch (Exception ex) {
            cnt = -1;
            ex.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return cnt;
    }
    /**
     * 导出
     *
     * @param file csv文件(路径+文件名)，csv文件不存在会自动创建
     * @param dataList 数据
     * @return
     */
    public static boolean exportCsv(File file, List<String> dataList){
        boolean isSucess=false;

        BufferedWriter bw=null;
        FileWriter writer = null;
        try {
            writer = new FileWriter(file,true);
            bw =new BufferedWriter(writer);
            if(dataList!=null && !dataList.isEmpty()){
                for(String data : dataList){
                    bw.write(data);
                    bw.newLine();
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.flush();
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(writer!=null){
                try {
                    writer.close();
                    writer=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }

    /**
     * 导出
     *
     * @param file csv文件(路径+文件名)，csv文件不存在会自动创建
     * @param array 数据
     * @return
     */
    public static boolean exportCsv(File file, JSONArray array,String exportHeader, String[] keys){
        boolean isSucess=false;

        BufferedWriter bw=null;
        FileWriter writer = null;
        try {
            writer = new FileWriter(file,true);
            bw =new BufferedWriter(writer);
            bw.write(exportHeader);
            bw.newLine();
            if(array!=null && !array.isEmpty()){
                StringBuffer bufferStr = new StringBuffer();
                for(int i=0;i<array.size();i++){
                    if(bufferStr.length()>0)
                        bufferStr.delete(0,bufferStr.length());
                    JSONObject object = array.getJSONObject(i);
                    for(int j=0;j<keys.length;j++){
                        bufferStr.append(object.getString(keys[j])).append(",");
                    }
                    if(bufferStr.length()>0)bufferStr.deleteCharAt(bufferStr.length()-1);
                    bw.write(bufferStr.toString());
                    bw.newLine();
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.flush();
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(writer!=null){
                try {
                    writer.close();
                    writer=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }

    /**
     * 导入
     *
     * @param file csv文件(路径+文件)
     * @return
     */
    public static List<String> importCsv(File file,int offset,int limit){
        List<String> dataList=new ArrayList<String>();
        LineNumberReader lr = null;
        try {
//            lr = new LineNumberReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
            lr = new LineNumberReader(new InputStreamReader(new FileInputStream(file)));
            int maxNumber = offset+limit;
            String line = null;
            while ((line = lr.readLine()) != null) {
                if(lr.getLineNumber()<offset)continue;
                if(lr.getLineNumber()>=maxNumber||line==null)break;
                dataList.add(line);
            }
        }catch (Exception e) {
        }finally{
            if(lr!=null){
                try {
                    lr.close();
                    lr=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return dataList;
    }
}